package com.xzakota.minesweeper.ui.dialog;

import com.xzakota.minesweeper.contant.ConstantPool;
import com.xzakota.minesweeper.controller.Game;
import com.xzakota.minesweeper.model.entity.LevelBean;
import com.xzakota.minesweeper.ui.dialog.templae.AbstractBaseDialog;
import com.xzakota.minesweeper.util.JSONFileUtils;
import com.xzakota.minesweeper.widget.text.InputTextField;

import javax.swing.*;
import java.awt.*;

/**
 * 自定义难度对话框
 *
 * @author Xzakota
 */
public class CustomLevelDialog extends AbstractBaseDialog {
    public CustomLevelDialog() {
        setTitle("自定义难度");
        setMinimumSize(new Dimension(250, 200));

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

        InputTextField heightTextField = new InputTextField("高度(9-24): ", false);
        heightTextField.setFieldText(JSONFileUtils.getInt(ConstantPool.SETTINGS_CUSTOM_LEVEL_H, LevelBean.LEVEL_BASIC.maxY) + "");
        mainPanel.add(heightTextField);

        InputTextField widthTextField = new InputTextField("宽度(9-30): ", false);
        widthTextField.setFieldText(JSONFileUtils.getInt(ConstantPool.SETTINGS_CUSTOM_LEVEL_W, LevelBean.LEVEL_BASIC.maxX) + "");
        mainPanel.add(widthTextField);

        InputTextField mineAmountTextField = new InputTextField("雷数(10-668): ", false);
        mineAmountTextField.setFieldText(JSONFileUtils.getInt(ConstantPool.SETTINGS_CUSTOM_LEVEL_M, LevelBean.LEVEL_BASIC.mineAmount) + "");
        mainPanel.add(mineAmountTextField);

        setComponent(mainPanel);

        style3Buttons();
        setConfirmAction(e -> {
            heightTextField.setHintText("");
            widthTextField.setHintText("");
            mineAmountTextField.setHintText("");

            int maxY = parseInt(heightTextField.getFieldText());
            int maxX = parseInt(widthTextField.getFieldText());
            int mineAmount = parseInt(mineAmountTextField.getFieldText());

            if (maxY < 9 || maxY > 24) {
                heightTextField.setHintText("高度不合法", Color.RED);
                return;
            }

            if (maxX < 9 || maxX > 30) {
                widthTextField.setHintText("宽度不合法", Color.RED);
                return;
            }

            int maxMineAmount = (int) Math.ceil(maxX * maxY * 0.940322 - 9.03225);

            if (mineAmount < 10 || mineAmount > 668) {
                mineAmountTextField.setHintText("雷数不合法", Color.RED);
                return;
            } else if (mineAmount > maxMineAmount) {
                mineAmountTextField.setHintText("雷数超出范围 10-" + maxMineAmount, Color.RED);
                return;
            }

            LevelBean customLevel = new LevelBean();
            customLevel.maxX = maxX;
            customLevel.maxY = maxY;
            customLevel.mineAmount = mineAmount;
            Game.dispatchEvent("setCustomLevel", customLevel);

            closeWindow();
        });
    }

    private int parseInt(String input) {
        try {
            return Integer.parseInt(input);
        } catch (NumberFormatException e) {
            return -1;
        }
    }
}
